home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  13.8 KB  |  404 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct MemoryCallbackUserData {
  31.     APTR ud_List;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. static __asm __saveds LONG memlist_con2func(register __a2 Object *obj, register __a1 struct NList_ConstructMessage *msg, register __a0 struct Hook *hook)
  36. {
  37.     return AllocListEntry(msg->pool, msg->entry, sizeof(struct MemoryEntry));
  38. }
  39.  
  40. MakeHook(memlist_con2hook, memlist_con2func);
  41.  
  42. static __asm __saveds LONG memlist_des2func(register __a2 Object *obj, register __a1 struct NList_DestructMessage *msg, register __a0 struct Hook *hook)
  43. {
  44.     FreeListEntry(msg->pool, &msg->entry);
  45.  
  46.     return 0;
  47. }
  48.  
  49. MakeHook(memlist_des2hook, memlist_des2func);
  50.  
  51. static __asm __saveds LONG memlist_dsp2func(register __a2 Object *obj, register __a1 struct NList_DisplayMessage *msg, register __a0 struct Hook *hook)
  52. {
  53.     struct MemoryEntry *me = (struct MemoryEntry *)msg->entry;
  54.  
  55.     if (me) {
  56.         msg->strings[0] = me->me_Address;
  57.         msg->strings[1] = me->me_Name;
  58.         msg->strings[2] = me->me_Type;
  59.         msg->strings[3] = me->me_Pri;
  60.         msg->strings[4] = me->me_Lower;
  61.         msg->strings[5] = me->me_Upper;
  62.         msg->strings[6] = me->me_Attributes;
  63.     } else {
  64.         msg->strings[0] = "Address";
  65.         msg->strings[1] = "ln_Name";
  66.         msg->strings[2] = "ln_Type";
  67.         msg->strings[3] = "ln_Pri";
  68.         msg->strings[4] = "mh_Lower";
  69.         msg->strings[5] = "mh_Upper";
  70.         msg->strings[6] = "mh_Attr";
  71.         msg->preparses[0] = MUIX_B;
  72.         msg->preparses[1] = MUIX_B;
  73.         msg->preparses[2] = MUIX_B;
  74.         msg->preparses[3] = MUIX_B;
  75.         msg->preparses[4] = MUIX_B;
  76.         msg->preparses[5] = MUIX_B;
  77.         msg->preparses[6] = MUIX_B;
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
  83. MakeHook(memlist_dsp2hook, memlist_dsp2func);
  84.  
  85. static LONG memlist_cmp2colfunc( struct MemoryEntry *me1,
  86.                                  struct MemoryEntry *me2,
  87.                                  ULONG column )
  88. {
  89.     LONG pri1, pri2;
  90.  
  91.     switch (column) {
  92.         case 0: return stricmp(me1->me_Address, me2->me_Address);
  93.         case 1: return stricmp(me1->me_Name, me2->me_Name);
  94.         case 2: return stricmp(me1->me_Type, me2->me_Type);
  95.         case 3: IsDec(me1->me_Pri, &pri1); IsDec(me2->me_Pri, &pri2); return pri2 - pri1;
  96.         case 4: return stricmp(me1->me_Lower, me2->me_Lower);
  97.         case 5: return stricmp(me1->me_Upper, me2->me_Upper);
  98.         case 6: return stricmp(me1->me_Attributes, me2->me_Attributes);
  99.     }
  100. }
  101.  
  102. static __asm __saveds LONG memlist_cmp2func(register __a2 Object *obj, register __a1 struct NList_CompareMessage *msg, register __a0 struct Hook *hook)
  103. {
  104.     LONG cmp;
  105.     struct MemoryEntry *me1, *me2;
  106.     ULONG col1, col2;
  107.  
  108.     me1 = (struct MemoryEntry *)msg->entry1;
  109.     me2 = (struct MemoryEntry *)msg->entry2;
  110.     col1 = msg->sort_type & MUIV_NList_TitleMark_ColMask;
  111.     col2 = msg->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  112.  
  113.     if (msg->sort_type == MUIV_NList_SortType_None) return 0;
  114.  
  115.     if (msg->sort_type & MUIV_NList_TitleMark_TypeMask) {
  116.         cmp = memlist_cmp2colfunc(me2, me1, col1);
  117.     } else {
  118.         cmp = memlist_cmp2colfunc(me1, me2, col1);
  119.     }
  120.  
  121.     if (cmp != 0 || col1 == col2) return cmp;
  122.  
  123.     if (msg->sort_type2 & MUIV_NList_TitleMark2_TypeMask) {
  124.         cmp = memlist_cmp2colfunc(me2, me1, col2);
  125.     } else {
  126.         cmp = memlist_cmp2colfunc(me1, me2, col2);
  127.     }
  128.  
  129.     return cmp;
  130. }
  131.  
  132. MakeHook(memlist_cmp2hook, memlist_cmp2func);
  133.  
  134. static void ReceiveList( void (* callback)( struct MemoryEntry *me, void *userData ),
  135.                          void *userData )
  136. {
  137.     struct MemoryEntry *me;
  138.  
  139.     if (me = tbAllocVecPooled(globalPool, sizeof(struct MemoryEntry))) {
  140.         if (SendDaemon("GetMemList")) {
  141.             while (ReceiveDecodedEntry((UBYTE *)me, sizeof(struct MemoryEntry))) {
  142.                 callback(me, userData);
  143.             }
  144.         }
  145.  
  146.         tbFreeVecPooled(globalPool, me);
  147.     }
  148. }
  149.  
  150. static void IterateList( void (* callback)( struct MemoryEntry *me, void *userData ),
  151.                          void *userData )
  152. {
  153.     struct MinList tmplist;
  154.     struct MemoryEntry *me, *_me;
  155.     struct MemHeader *mh;
  156.  
  157.     NewList((struct List *)&tmplist);
  158.  
  159.     Forbid();
  160.  
  161.     ITERATE_LIST(&SysBase->MemList, struct MemHeader *, mh) {
  162.         if (me = AllocVec(sizeof(struct MemoryEntry), MEMF_PUBLIC)) {
  163.             me->me_Header = mh;
  164.             _snprintf(me->me_Address, sizeof(me->me_Address), "$%08lx", mh);
  165.             stccpy(me->me_Name, nonetest(mh->mh_Node.ln_Name), sizeof(me->me_Name));
  166.             stccpy(me->me_Type, GetNodeType(mh->mh_Node.ln_Type), sizeof(me->me_Type));
  167.             _snprintf(me->me_Pri, sizeof(me->me_Pri), "%4ld", mh->mh_Node.ln_Pri);
  168.             _snprintf(me->me_Lower, sizeof(me->me_Lower), "$%08lx", mh->mh_Lower);
  169.             _snprintf(me->me_Upper, sizeof(me->me_Upper), "$%08lx", mh->mh_Upper);
  170.             _snprintf(me->me_Attributes, sizeof(me->me_Attributes), "$%04lx", mh->mh_Attributes);
  171.  
  172.             AddTail((struct List *)&tmplist, (struct Node *)me);
  173.         }
  174.     }
  175.  
  176.     Permit();
  177.  
  178.     ITERATE_CHANGING_LIST(&tmplist, struct MemoryEntry *, me, _me) {
  179.         callback(me, userData);
  180.         FreeVec(me);
  181.     }
  182. }
  183.  
  184. static void UpdateCallback( struct MemoryEntry *me,
  185.                             void *userData )
  186. {
  187.     struct MemoryCallbackUserData *ud = (struct MemoryCallbackUserData *)userData;
  188.  
  189.     InsertBottomEntry(ud->ud_List, me);
  190.     ud->ud_Count++;
  191. }
  192.  
  193. static void PrintCallback( struct MemoryEntry *me,
  194.                            void *userData )
  195. {
  196.     PrintFOneLine((BPTR)userData, " %s %-20.20s %-8s%4s %s %s %s\n", me->me_Address, me->me_Name, me->me_Type, me->me_Pri, me->me_Lower, me->me_Upper, me->me_Attributes);
  197. }
  198.  
  199. static void SendCallback( struct MemoryEntry *me,
  200.                           void *userData )
  201. {
  202.     SendEncodedEntry((UBYTE *)me, sizeof(struct MemoryEntry));
  203. }
  204.  
  205. static ULONG __saveds mNew( struct IClass *cl,
  206.                             Object *obj,
  207.                             struct opSet *msg )
  208. {
  209.     APTR memlist, memtext, updateButton, printButton, priorityButton, moreButton, exitButton;
  210.  
  211.     if (obj = (Object *)DoSuperNew(cl, obj,
  212.         MUIA_HelpNode, MemoryText,
  213.         MUIA_Window_ID, MakeID('M','E','M','O'),
  214.         WindowContents, VGroup,
  215.  
  216.             Child, memlist = MyNListviewObject(MakeID('M','E','L','V'), "BAR,BAR,BAR P=" MUIX_C ",BAR P=" MUIX_R ",BAR,BAR,BAR", &memlist_con2hook, &memlist_des2hook, &memlist_dsp2hook, &memlist_cmp2hook, TRUE),
  217.             Child, memtext = MyTextObject(),
  218.  
  219.             Child, MyVSpace(4),
  220.  
  221.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  222.                 Child, updateButton   = MakeButton(txtUpdate),
  223.                 Child, printButton    = MakeButton(txtPrint),
  224.                 Child, priorityButton = MakeButton(txtPriority),
  225.                 Child, moreButton     = MakeButton(txtMore),
  226.                 Child, exitButton     = MakeButton(txtExit),
  227.             End,
  228.         End,
  229.         TAG_MORE, msg->ops_AttrList))
  230.     {
  231.         struct MemoryWinData *mwd = INST_DATA(cl, obj);
  232.         APTR parent;
  233.  
  234.         mwd->mwd_MemoryList = memlist;
  235.         mwd->mwd_MemoryText = memtext;
  236.         mwd->mwd_PriorityButton = priorityButton;
  237.         mwd->mwd_MoreButton = moreButton;
  238.  
  239.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  240.  
  241.         set(obj, MUIA_Window_Title, MyGetWindowTitle("MEMORY", mwd->mwd_Title, sizeof(mwd->mwd_Title)));
  242.         set(obj, MUIA_Window_ActiveObject, memlist);
  243.         set(priorityButton, MUIA_Disabled, TRUE);
  244.         set(moreButton, MUIA_Disabled, TRUE);
  245.  
  246.         DoMethod(parent,         MUIM_Window_AddChildWindow, obj);
  247.         DoMethod(obj,            MUIM_Notify, MUIA_Window_CloseRequest, TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  248.         DoMethod(memlist,        MUIM_Notify, MUIA_NList_Active,        MUIV_EveryTime, obj,                     1, MUIM_MemoryWin_ListChange);
  249.         DoMethod(memlist,        MUIM_Notify, MUIA_NList_DoubleClick,   MUIV_EveryTime, obj,                     1, MUIM_MemoryWin_More);
  250.         DoMethod(updateButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_MemoryWin_Update);
  251.         DoMethod(printButton,    MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_MemoryWin_Print);
  252.         DoMethod(priorityButton, MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_MemoryWin_Priority);
  253.         DoMethod(moreButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_MemoryWin_More);
  254.         DoMethod(exitButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  255.     }
  256.  
  257.     return (ULONG)obj;
  258. }
  259.  
  260. static ULONG __saveds mDispose( struct IClass *cl,
  261.                                 Object *obj,
  262.                                 struct opSet *msg )
  263. {
  264.     struct MemoryWinData *mwd = INST_DATA(cl, obj);
  265.  
  266.     set(obj, MUIA_Window_Open, FALSE);
  267.     DoMethod(mwd->mwd_MemoryList, MUIM_NList_Clear);
  268.  
  269.     return (DoSuperMethodA(cl, obj, msg));
  270. }
  271.  
  272. static ULONG __saveds mUpdate( struct IClass *cl,
  273.                                Object *obj,
  274.                                Msg msg )
  275. {
  276.     struct MemoryWinData *mwd = INST_DATA(cl, obj);
  277.     struct MemoryCallbackUserData ud;
  278.  
  279.     ApplicationSleep(TRUE);
  280.     set(mwd->mwd_MemoryList, MUIA_NList_Quiet, TRUE);
  281.     DoMethod(mwd->mwd_MemoryList, MUIM_NList_Clear);
  282.  
  283.     ud.ud_List = mwd->mwd_MemoryList;
  284.     ud.ud_Count = 0;
  285.  
  286.     if (clientstate) {
  287.         ReceiveList(UpdateCallback, &ud);
  288.     } else {
  289.         IterateList(UpdateCallback, &ud);
  290.     }
  291.  
  292.     MySetContents(mwd->mwd_MemoryText, "");
  293.  
  294.     set(mwd->mwd_MemoryList, MUIA_NList_Quiet, FALSE);
  295.     set(mwd->mwd_PriorityButton, MUIA_Disabled, TRUE);
  296.     set(mwd->mwd_MoreButton, MUIA_Disabled, TRUE);
  297.     ApplicationSleep(FALSE);
  298.  
  299.     return 0;
  300. }
  301.  
  302. static ULONG __saveds mPrint( struct IClass *cl,
  303.                               Object *obj,
  304.                               Msg msg )
  305. {
  306.     PrintMemory(NULL);
  307.  
  308.     return 0;
  309. }
  310.  
  311. static ULONG __saveds mPriority( struct IClass *cl,
  312.                                  Object *obj,
  313.                                  Msg msg )
  314. {
  315.     struct MemoryWinData *mwd = INST_DATA(cl, obj);
  316.     struct MemoryEntry *me;
  317.  
  318.     if (me = (struct MemoryEntry *)GetActiveEntry(mwd->mwd_MemoryList)) {
  319.         LONG pri;
  320.  
  321.         pri = atol(me->me_Pri);
  322.         if (GetPriority(me->me_Name, &pri)) {
  323.             if (MyDoCommand("SetPriority MEMORY \"%s\" %ld", me->me_Name, pri)) {
  324.                 _snprintf(me->me_Pri, sizeof(me->me_Pri), "%4ld", pri);
  325.                 RedrawActiveEntry(mwd->mwd_MemoryList);
  326.             }
  327.         }
  328.     }
  329.  
  330.     return 0;
  331. }
  332.  
  333. static ULONG __saveds mMore( struct IClass *cl,
  334.                              Object *obj,
  335.                              Msg msg )
  336. {
  337.     struct MemoryWinData *mwd = INST_DATA(cl, obj);
  338.     struct MemoryEntry *me;
  339.  
  340.     if (me = (struct MemoryEntry *)GetActiveEntry(mwd->mwd_MemoryList)) {
  341.         APTR detailWin;
  342.  
  343.         if (detailWin = MemoryDetailWindowObject,
  344.                 MUIA_Window_ParentWindow, obj,
  345.             End) {
  346.             set(detailWin, MUIA_MemoryDetailWin_Memory, me);
  347.             set(detailWin, MUIA_Window_Open, TRUE);
  348.         }
  349.     }
  350.  
  351.     return 0;
  352. }
  353.  
  354. static ULONG __saveds mListChange( struct IClass *cl,
  355.                                    Object *obj,
  356.                                    Msg msg )
  357. {
  358.     struct MemoryWinData *mwd = INST_DATA(cl, obj);
  359.     struct MemoryEntry *me;
  360.  
  361.     if (me = (struct MemoryEntry *)GetActiveEntry(mwd->mwd_MemoryList)) {
  362.         MySetContents(mwd->mwd_MemoryText, "%s \"%s\"", me->me_Address, me->me_Name);
  363.         set(mwd->mwd_PriorityButton, MUIA_Disabled, FALSE);
  364.         if (!clientstate) set(mwd->mwd_MoreButton, MUIA_Disabled, FALSE);
  365.     }
  366.  
  367.     return 0;
  368. }
  369.  
  370. ULONG __asm __saveds MemoryWinDispatcher( register __a0 struct IClass *cl,
  371.                                              register __a2 Object *obj,
  372.                                              register __a1 Msg msg )
  373. {
  374.     switch (msg->MethodID) {
  375.         case OM_NEW:                    return (mNew(cl, obj, (APTR)msg));
  376.         case OM_DISPOSE:                return (mDispose(cl, obj, (APTR)msg));
  377.         case MUIM_MemoryWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  378.         case MUIM_MemoryWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  379.         case MUIM_MemoryWin_Priority:   return (mPriority(cl, obj, (APTR)msg));
  380.         case MUIM_MemoryWin_More:       return (mMore(cl, obj, (APTR)msg));
  381.         case MUIM_MemoryWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  382.     }
  383.  
  384.     return (DoSuperMethodA(cl, obj, msg));
  385. }
  386.  
  387. void PrintMemory( char *filename )
  388. {
  389.     BPTR handle;
  390.  
  391.     if (handle = HandlePrintStart(filename)) {
  392.         PrintFOneLine(handle, "\n  Address  Name                 Type     Pri    Lower     Upper  Attr\n\n");
  393.         IterateList(PrintCallback, (void *)handle);
  394.     }
  395.  
  396.     HandlePrintStop();
  397. }
  398.  
  399. void SendMemList( void )
  400. {
  401.     IterateList(SendCallback, NULL);
  402. }
  403.  
  404.